package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.admin.exception.BusinessException;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.dto.MessageDto;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.AdminCommentVO;
import com.tanhua.model.vo.MovementsVO;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Xiao
 * @version 1.0
 * @description
 * @date 2021/11/3
 */
@Service
public class MessagesService {
    @DubboReference
    private MovementApi movementApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private CommentApi commentApi;
    /**
     * 动态分页
     * @param messageDto    pagesize、page、uid、state
     * @return
     */
    public PageResult getMessages(MessageDto messageDto) {
        PageResult pageResult = new PageResult();
        if (ObjectUtil.isEmpty(messageDto)) {
            return pageResult;
        }
        Integer page = messageDto.getPage();
        Integer pagesize = messageDto.getPagesize();
        Long uid = messageDto.getUid();
        String state = messageDto.getState();
        state = (state == null || state.equals("''")) ? null : state;
        pageResult = movementApi.getMessages(page, pagesize, uid, state);
        List<Movement> movements = (List<Movement>) pageResult.getItems();
        if (CollUtil.isEmpty(movements)) {
            return pageResult;
        }
        List<Long> userIds = CollUtil.getFieldValues(movements, "userId", Long.class);
        Map<Long, UserInfo> infoMap = userInfoApi.selectByIdsAndCondition(userIds, null);
        List<MovementsVO> movementsVOs = new ArrayList<>();
        for (Movement movement : movements) {
            UserInfo userInfo = infoMap.get(movement.getUserId());
            if (ObjectUtil.isNotEmpty(userInfo)) {
                movementsVOs.add(MovementsVO.init(userInfo, movement));
            }
        }
        pageResult.setItems(movementsVOs);
        return pageResult;
    }

    /**
     * 查询动态详情
     * @param id
     * @return
     */
    public MovementsVO getMessage(String id) {
        Movement movement = movementApi.findById(id);
        UserInfo userInfo = userInfoApi.selectById(movement.getUserId());
        return MovementsVO.init(userInfo, movement);

    }

    /**
     * 评论列表分页查询
     * @param page
     * @param pagesize
     * @param messageID 动态id
     * @return
     */
    public PageResult getComments(Integer page, Integer pagesize, String messageID) {
        PageResult pageResult = commentApi.getComments(page, pagesize, messageID);
        List<Comment> comments = (List<Comment>) pageResult.getItems();
        if (CollUtil.isEmpty(comments)) {
            return pageResult;
        }
        List<Long> userIds = CollUtil.getFieldValues(comments, "userId", Long.class);
        Map<Long, UserInfo> infoMap = userInfoApi.selectByIdsAndCondition(userIds, null);
        List<AdminCommentVO> adminCommentVOs = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = infoMap.get(comment.getUserId());
            if (ObjectUtil.isNotEmpty(userInfo)) {
                adminCommentVOs.add(AdminCommentVO.init(userInfo, comment));
            }
        }
        pageResult.setItems(adminCommentVOs);
        return pageResult;
    }

    /**
     * 动态审核
     * @param ids   动态id列表
     * @param state 1-通过、2-驳回
     * @return
     */
    public String checkMessage(String[] ids, Integer state) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException("未选择动态！");
        }
        List<ObjectId> movementIds = Arrays.stream(ids).map(ObjectId::new).collect(Collectors.toList());
        return movementApi.updateState(movementIds, state);
    }
}
